package com.museum.service.impl.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.museum.base.BaseApiService;
import com.museum.base.BaseResponse;
import com.museum.controller.api.admin.AdminAreaAPI;
import com.museum.core.utils.JudgeInt;
import com.museum.entity.DO.Area;
import com.museum.entity.DO.AreaPermission;
import com.museum.entity.DTO.area.AreaDTO;
import com.museum.entity.DTO.area.addArea;
import com.museum.entity.DTO.area.queryArea;
import com.museum.mapper.AreaMapper;
import com.museum.mapper.AreaPermissionMapper;
import com.museum.mapper.PermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LB
 * @version 1.0
 * @date 2021/1/4 16:43
 */
@Service
public class AdminAreaServiceImpl extends BaseApiService<JSONObject> implements AdminAreaAPI {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private AreaPermissionMapper areaPermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public BaseResponse<JSONObject> addArea(addArea areaDTO) {
        String areaName = areaDTO.getAreaName();
        List<Area> areaList = areaMapper.selectList(null);
        for (Area area : areaList) {
            if (area.getAreaName().equals(areaName)) {
                return setResult(201, "此地域已存在", null);
            }
        }
        Integer pId = areaDTO.getParentId();
        Area area = new Area();
        BeanUtils.copyProperties(areaDTO, area);
        if (pId == null || pId == 0) {
            //没有父级部门的Id，默认是省级部门
            area.setParentId(0);
        } else {
            area.setParentId(areaDTO.getParentId());
        }
        int insert = areaMapper.insert(area);
        int areaNumber = area.getAreaId();
        area.setAreaNumber(areaNumber);
        areaMapper.updateById(area);
        //根据地域Number 分配地域所有的权限
        List<Integer> ids = permissionMapper.selectPermissionIds();
        for (int i = 0; i < ids.size(); i++) {
            AreaPermission ap = new AreaPermission();
            ap.setStatus(0);
            ap.setAreaNumber(area.getAreaNumber());
            ap.setPermissionId(ids.get(i));
            areaPermissionMapper.insert(ap);
        }
        if (insert > 0) {
            return setResult(200, "地域添加成功", null);
        } else {
            return setResult(201, "系统错误,请重试", null);
        }
    }

    public BaseResponse<JSONObject> updateArea(Area areaUpdate) {
        if (areaUpdate.getAreaId() != null) {
            areaMapper.updateById(areaUpdate);
        }
        return setResultSuccess("修改成功");
    }


    @Override
    public BaseResponse<JSONObject> deleteArea(Integer number) {
        Integer count = areaMapper.selectCount(new QueryWrapper<Area>().eq("area_number", number));
        if (count == 0) {
            return setResult(201, "无此地域,请重试", null);
        }
        int areaId = areaMapper.selectIdByNumber(number);
        int result = areaMapper.selectCountById(areaId);
        Area area = areaMapper.selectById(areaId);
        if (result > 0) {
            //说明有子孩子
            return setResult(201, "请先删除其附属地域", null);
        } else {
            int i = areaMapper.deleteById(area);
            //删除权限
            List<Integer> ids = areaPermissionMapper.selectPermissionIds(number);
            if (ids.size() > 0) {
                areaPermissionMapper.delete(new QueryWrapper<AreaPermission>().eq("area_number", number));

            }
            return setResult(200, "删除成功", null);
        }
    }


    @Override
    public BaseResponse<JSONObject> getAllArea() {
        JSONObject jsonObject = new JSONObject();
        //先获得树形的地区表
        List<Area> areaList = areaMapper.selectList(null);
        List<queryArea> areas = new ArrayList<>();
        int i = 0;
        for (Area area : areaList) {
            queryArea queryArea = new queryArea();
            BeanUtils.copyProperties(area, queryArea);
            areas.add(queryArea);
        }
        //拥有了封装好的地域表了 进行树构建
        List<queryArea> buildTrees = build(areas);
        //丰富每一个地区的权限
        jsonObject.put("areaList", buildTrees);
        return setResultSuccess(jsonObject);
    }

    private List<queryArea> build(List<queryArea> areas) {
        List<queryArea> list = new ArrayList<>();
        for (queryArea area : areas) {
            if (area.getParentId() == 0) {
                list.add(findChild(area, areas));
            }
        }
        return list;
    }

    private queryArea findChild(queryArea area, List<queryArea> areas) {
        area.setChildren(new ArrayList<queryArea>());
        for (queryArea queryArea : areas) {
            if (queryArea.getParentId().equals(area.getAreaId())) {
                area.getChildren().add(findChild(queryArea, areas));
            }
        }
        return area;
    }
}
